

#import "PhotoMaskView.h"

@implementation PhotoMaskView
{
    CGRect _squareRect;   // 外切正方形
    CGFloat _cropWidth;
    CGFloat _cropHeight;
}

-(instancetype)initWithFrame:(CGRect)frame width:(CGFloat)cropWidth height:(CGFloat)height
{
    self = [super initWithFrame:frame];
    if (self) {
        self.mode = PhotoMaskViewModeCircle;
        self.backgroundColor = [UIColor clearColor];
        _cropWidth = cropWidth;
        _cropHeight = height;
        self.isDark = NO;
        self.lineColor = [UIColor whiteColor];
    }
    return self;
}

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    if (self.mode == PhotoMaskViewModeCircle) {
        
     [self crop:rect];
    }else{
        [self crop2:rect];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(layoutScrollViewWithRect:)]) {
        [self.delegate layoutScrollViewWithRect:_squareRect];
    }
}

//-(void)crop2:(CGRect)rect
//{
//    CGFloat width = rect.size.width;
//    CGFloat height = rect.size.height;
//    CGContextRef contextRef = UIGraphicsGetCurrentContext();
//    CGContextSaveGState(contextRef);
//    CGContextSetRGBFillColor(contextRef, 0, 0, 0, 0.4);
//   _squareRect = CGRectMake((width - _cropWidth) / 2, (height - _cropHeight) / 2, _cropWidth, _cropHeight);
//    UIBezierPath *squarePath = [UIBezierPath bezierPathWithRect:_squareRect];
//    UIBezierPath *maskBezierPath = [UIBezierPath bezierPathWithRect:rect];
//    [maskBezierPath appendPath:squarePath];
//    maskBezierPath.usesEvenOddFillRule = YES;
//    [maskBezierPath fill];
//
//    CGFloat x =  rect.size.width/3;
//    CGPoint addLines[4];
//    addLines[0] = CGPointMake(x, 0);
//    addLines[1] = CGPointMake(x, rect.size.width);
//    CGContextAddLines(contextRef, addLines, 2);
//
//    CGContextSetLineWidth(contextRef, 2);
//    if (self.isDark) {
//        CGFloat length[2] = {5,5};
//        CGContextSetLineDash(contextRef, 0, length, 2);
//    }
//    CGContextSetStrokeColorWithColor(contextRef, _lineColor.CGColor);
//    [squarePath stroke];
//    CGContextRestoreGState(contextRef);
//    self.layer.contentsGravity = kCAGravityCenter;
//}
-(void)crop2:(CGRect)rect
{
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGContextSaveGState(contextRef);
    _squareRect = CGRectMake((width - _cropWidth) / 2, (height - _cropHeight) / 2, _cropWidth, _cropHeight);

    CGFloat x =  rect.size.width/3;
    CGPoint addLines[2];
    addLines[0] = CGPointMake(x, 0);
    addLines[1] = CGPointMake(x, rect.size.width);
    CGContextAddLines(contextRef, addLines, 2);
    
    addLines[0] = CGPointMake(2*x, 0);
    addLines[1] = CGPointMake(2*x, rect.size.width);
    CGContextAddLines(contextRef, addLines, 2);
    
    
    addLines[0] = CGPointMake(0, x);
    addLines[1] = CGPointMake(rect.size.width,x);
    CGContextAddLines(contextRef, addLines, 2);
    
    addLines[0] = CGPointMake(0,2*x);
    addLines[1] = CGPointMake(rect.size.width,2*x);
    CGContextAddLines(contextRef, addLines, 2);
    
    CGContextSetLineWidth(contextRef, 2);
    
    [@"拖拽进行剪切" drawAtPoint:CGPointMake(rect.size.width/2-45, rect.size.height/2) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:UIColor.whiteColor}];
//    [@"拖拽进行剪切" drawInRect:CGRectMake(rect.size.width/2 - 60, rect.size.height/2-15, 120, 30) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:UIColor.whiteColor}];
    
    CGContextSetStrokeColorWithColor(contextRef, _lineColor.CGColor);
    CGContextDrawPath(contextRef, kCGPathStroke);
    
    CGContextRestoreGState(contextRef);
    self.layer.contentsGravity = kCAGravityCenter;
}


-(void)crop:(CGRect)rect
{
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGContextSaveGState(contextRef);
    CGContextSetRGBFillColor(contextRef, 0, 0, 0, 0.4);
    _squareRect = CGRectMake((width - _cropWidth) / 2, (height - _cropWidth) / 2, _cropWidth, _cropWidth);
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:_squareRect];
    UIBezierPath *maskBezierPath = [UIBezierPath bezierPathWithRect:rect];
    [maskBezierPath appendPath:circlePath];
    maskBezierPath.usesEvenOddFillRule = YES;
    [maskBezierPath fill];
    
    
    
    CGContextSetLineWidth(contextRef, 2);
    if (self.isDark) {
        CGFloat length[2] = {5,5};
        CGContextSetLineDash(contextRef, 0, length, 2);
    }
    
    
    
    CGContextSetStrokeColorWithColor(contextRef, _lineColor.CGColor);
    
    [circlePath stroke];
    CGContextRestoreGState(contextRef);
    self.layer.contentsGravity = kCAGravityCenter;
}
@end
